<template>
  <div class="admin-home">
    <!-- 数据统计卡片区域 -->
    <div class="stats-cards">
      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon blue">
            <el-icon><User /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalUsers }}</div>
            <div class="stat-label">总用户数</div>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon green">
            <el-icon><Document /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalCourses }}</div>
            <div class="stat-label">课程总数</div>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon purple">
            <el-icon><VideoPlay /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalVideos }}</div>
            <div class="stat-label">视频数量</div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {
  User,
  Document,
  VideoPlay
} from '@element-plus/icons-vue'
import axios from 'axios'

// 统计数据
const stats = ref({
  totalUsers: 0,
  totalCourses: 0,
  totalVideos: 0
})

// 加载数据
const loadData = async () => {
  try {
    // 从API获取用户总数
    const userResponse = await axios.get('/api/admin/user/count')
    if (userResponse.data.code === 100) {
      stats.value.totalUsers = userResponse.data.data
    }
    
    // 从API获取课程总数
    const courseResponse = await axios.get('/api/admin/course/count')
    if (courseResponse.data.code === 100) {
      stats.value.totalCourses = courseResponse.data.data
    }
    
    // 从API获取视频总数
    const videoResponse = await axios.get('/api/admin/video/count')
    if (videoResponse.data.code === 100) {
      stats.value.totalVideos = videoResponse.data.data
    }
  } catch (error) {
    console.error('Failed to load dashboard data:', error)
  }
}

onMounted(() => {
  // 加载数据
  loadData()
})
</script>

<style lang="scss" scoped>
.admin-home {
  min-height: 100vh;
  background-color: #ecf0f5;
  padding: 20px;
  padding-left: 240px; /* 为左侧菜单留出空间 */
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
  
  /* 响应式调整 */
  @media (max-width: 768px) {
    padding-left: 80px;
  }
}

/* 统计卡片样式 */
.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 25px;
  width: 100%;
  max-width: 1000px;
}

.stat-card {
  border: none;
  border-radius: 10px;
  transition: all 0.3s ease;
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 12px 20px rgba(0, 0, 0, 0.1);
  }
  
  .stat-content {
    display: flex;
    align-items: center;
    gap: 20px;
    padding: 25px 0;
  }
  
  .stat-icon {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 28px;
    
    &.blue {
      background-color: #3498db;
    }
    
    &.green {
      background-color: #2ecc71;
    }
    
    &.purple {
      background-color: #9b59b6;
    }
  }
  
  .stat-info {
    flex: 1;
  }
  
  .stat-value {
    font-size: 36px;
    font-weight: bold;
    color: #2c3e50;
    line-height: 1.1;
  }
  
  .stat-label {
    font-size: 16px;
    color: #7f8c8d;
    margin-top: 5px;
  }
}

/* 响应式布局优化 */
@media (max-width: 768px) {
  .stats-cards {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
  }
  
  .stat-card .stat-content {
    gap: 15px;
  }
  
  .stat-icon {
    width: 50px;
    height: 50px;
    font-size: 24px;
  }
  
  .stat-value {
    font-size: 32px;
  }
}

@media (max-width: 480px) {
  .admin-home {
    padding: 15px;
    padding-left: 80px;
  }
  
  .stats-cards {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .stat-card .stat-content {
    padding: 20px 0;
  }
}
</style>